package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.ExportExcelUtils;
import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.pojo.po.EtcmDiseaseTypePo;
import com.newviews.tcmip.pojo.po.EtcmHerbMeridiansPo;
import com.newviews.tcmip.pojo.vo.EtcmCommonBaseInfoRespVO;
import com.newviews.tcmip.pojo.vo.export.EtcmCommonExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmDiseaseDetailsExportReqVO;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmDiseaseServiceInf;
import com.newviews.tcmip.web.vo.etcmdiseases.EtcmDiseasesExportReqVO;
import com.newviews.tcmip.web.vo.etcmherb.statistics.HerbStatisticsRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 疾病基本信息 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/etcm/disease")
@Api(tags = "疾病信息")
public class EtcmDiseaseController {

    @Autowired
    private EtcmDiseaseServiceInf etcmDiseaseService;

    @Autowired
    private ConfigProperties configProperties;

    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    @GetMapping("/page")
    @ApiOperation("获取疾病首页数据分页信息")
    public ResultModel<PageModel<Map<String,String>>> getPage(@RequestParam(required = false) String content,
                                                              @RequestParam String langCode,
                                                              @RequestParam String uiTypeCode,
                                                              @RequestParam(required = false) String type,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
       return etcmDiseaseService.getPage(content, langCode, uiTypeCode, pageNo, pageSize, sortType,type);
    }

    @ApiOperation(value = "疾病：统计")
    @GetMapping("/statistics")
    public ResultModel<Map<String,String>> getStatistics(@RequestParam(required = false) String content){
        return etcmDiseaseService.getStatistics(content);
    }

    @GetMapping("/details")
    @ApiOperation("获取疾病基本信息分页信息")
    public ResultModel<List<EtcmCommonBaseInfoRespVO>>getDetails(@RequestParam String diseaseId,
                                                                     @RequestParam String langCode,
                                                                      @RequestParam String uiTypeCode,
                                                                     @RequestParam(required = false) String content){
        return etcmDiseaseService.getDetails(diseaseId, langCode, uiTypeCode,content);
    }

    @GetMapping("/herb")
    @ApiOperation("获取疾病草药相关信息")
    public ResultModel<PageModel<Map<String,String>>> getHerb(@RequestParam(required = false) String content,
                                                              @RequestParam String diseaseId,
                                                              @RequestParam String langCode,
                                                              @RequestParam String uiTypeCode,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmDiseaseService.getHerb(content, diseaseId, langCode, uiTypeCode, pageNo, pageSize, sortType);
    }

    @GetMapping("/prescription")
    @ApiOperation("获取疾病处方相关信息")
    public ResultModel<PageModel<Map<String,String>>> getPrescription(@RequestParam(required = false) String content,
                                                              @RequestParam String diseaseId,
                                                              @RequestParam String langCode,
                                                              @RequestParam String uiTypeCode,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "100") Integer pageSize,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmDiseaseService.getPrescription(content, diseaseId, langCode, uiTypeCode, pageNo, pageSize, sortType);
    }

    @GetMapping("/ingredient")
    @ApiOperation("获取疾病成分相关信息")
    public ResultModel<PageModel<Map<String,String>>> getIngredient(@RequestParam(required = false) String content,
                                                                      @RequestParam String diseaseId,
                                                                      @RequestParam String langCode,
                                                                      @RequestParam String uiTypeCode,
                                                                      @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                                      @RequestParam(required = false, defaultValue = "100") Integer pageSize,
                                                                      @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmDiseaseService.getIngr(content, diseaseId, langCode, uiTypeCode, pageNo, pageSize, sortType);
    }

    @PostMapping("/download")
    @ApiOperation(value = "疾病数据首页信息下载")
    public void downloadDisease(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseasesExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmDiseaseService.downloadDisease(reqVO.getType(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode());

            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/details/download")
    @ApiOperation(value = "疾病数据详情信息下载")
    public void downloadDetails(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseaseDetailsExportReqVO reqVO) {
        String path="";
        String filename="";
        try {
            List<Map<String, String>> pxDetails = etcmDiseaseService.getDiseaseDetails(reqVO.getDiseaseId(),reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode());
            File file=new File(configProperties.getFileExportPath());
            if(!file.exists()){
                file.mkdirs();
            }
            if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
                filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
                path= configProperties.getFileExportPath()+ filename;
                ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
            }else {
                filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
                path= configProperties.getFileExportPath()+ filename;
                ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
            }
            InputStream stream = new FileInputStream(path);
            response.setHeader("Content-Disposition", "attachment;"+"filename=" + new String(filename.getBytes(), "utf-8"));
            ServletOutputStream out = response.getOutputStream();
            byte buff[] = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) > 0) {
                out.write(buff, 0, length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            new File(path).delete();
        }
    }

    @PostMapping("/px/download")
    @ApiOperation(value = "疾病处方关联信息下载")
    public void downloadDiseasePx(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseaseDetailsExportReqVO reqVO) {
        String path="";
        try {
            List<Map<String, String>> pxDetails = etcmDiseaseService.getPrescriptionList(reqVO.getContent(),reqVO.getDiseaseId(), reqVO.getLangCode(), reqVO.getUiTypeCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxDetails);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/herb/download")
    @ApiOperation(value = "疾病草药关联信息下载")
    public void downloadDiseaseHerb(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseaseDetailsExportReqVO reqVO) {
        String path="";
        try {
            List<Map<String, String>> pxDetails = etcmDiseaseService.getHerbList(reqVO.getContent(),reqVO.getDiseaseId(), reqVO.getLangCode(), reqVO.getUiTypeCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxDetails);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/ingr/download")
    @ApiOperation(value = "疾病成分关联信息下载")
    public void downloadDiseaseIngr(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmDiseaseDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmDiseaseService.getIngrList(reqVO.getContent(),reqVO.getDiseaseId(), reqVO.getLangCode(), reqVO.getUiTypeCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),pxDetails);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "中草药库：字典-中草药分类")
    @GetMapping("/type")
    public ResultModel<List<EtcmDiseaseTypePo>> getDiseaseTypeList(@RequestParam @NotBlank(message = "语言编码不能为空") String langCode){
        return etcmDiseaseService.getDiseaseTypeList(langCode);
    }
}
